<p>When you run an OS command, it is always important to protect yourself against the risk of accidental or malicious replacement of the executables
in the production system.</p>
<p>To do so, it is important to point to the specific executable that should be used.</p>
<p>For example, if you call <code>git</code> (without specifying a path), the operating system will search for the executable in the directories
specified in the <code>PATH</code> environment variable.<br> An attacker could have added, in a permissive directory covered by <code>PATH</code> ,
another executable called <code>git</code>, but with a completely different behavior, for example exfiltrating data or exploiting a vulnerability in
your own code.</p>
<p>However, by calling <code>/usr/bin/git</code> or <code>../git</code> (relative path) directly, the operating system will always use the intended
executable.<br> Note that you still need to make sure that the executable is not world-writeable and potentially overwritten. This is not the scope of
this rule.</p>
<h2>Ask Yourself Whether</h2>
<ul>
  <li> The PATH environment variable only contains fixed, trusted directories. </li>
</ul>
<p>There is a risk if you answered no to this question.</p>
<h2>Recommended Secure Coding Practices</h2>
<p>If you wish to rely on the <code>PATH</code> environment variable to locate the OS command, make sure that each of its listed directories is fixed,
not susceptible to change, and not writable by unprivileged users.</p>
<p>If you determine that these folders cannot be altered, and that you are sure that the program you intended to use will be used, then you can
determine that these risks are under your control.</p>
<p>A good practice you can use is to also hardcode the <code>PATH</code> variable you want to use, if you can do so in the framework you use.</p>
<p>If the previous recommendations cannot be followed due to their complexity or other requirements, then consider using the absolute path of the
command instead.</p>
<pre>
$ whereis git
git: /usr/bin/git /usr/share/man/man1/git.1.gz
$ ls -l /usr/bin/git
-rwxr-xr-x 1 root root 3376112 Jan 28 10:13 /usr/bin/git
</pre>
<h2>Sensitive Code Example</h2>
<pre>
const cp = require('child_process');
cp.exec('file.exe'); // Sensitive
</pre>
<h2>Compliant Solution</h2>
<pre>
const cp = require('child_process');
cp.exec('/usr/bin/file.exe');
</pre>
<h2>See</h2>
<ul>
  <li> OWASP - <a href="https://owasp.org/Top10/A08_2021-Software_and_Data_Integrity_Failures/">Top 10 2021 Category A8 - Software and Data Integrity
  Failures</a> </li>
  <li> OWASP - <a href="https://owasp.org/www-project-top-ten/2017/A1_2017-Injection">Top 10 2017 Category A1 - Injection</a> </li>
  <li> CWE - <a href="https://cwe.mitre.org/data/definitions/426">CWE-426 - Untrusted Search Path</a> </li>
  <li> CWE - <a href="https://cwe.mitre.org/data/definitions/427">CWE-427 - Uncontrolled Search Path Element</a> </li>
</ul>
